<template>
  <common-layout>
    <div class="login">
      <a-form-model ref="changePwdForm" :model="form" :rules="rules">
        <a-tabs
          size="large"
          :tabBarStyle="{ textAlign: 'center' }"
          style="padding: 0 2px"
        >
          <a-tab-pane tab="修改密码" key="1">
            <a-alert
              type="error"
              :closable="true"
              v-show="error"
              :message="error"
              showIcon
              style="margin-bottom: 24px"
            />

            <a-form-model-item prop="newPassword">
              <a-input-password
                size="large"
                placeholder="请输入新密码"
                autocomplete="autocomplete"
                v-model="form.newPassword"
              >
              </a-input-password>
            </a-form-model-item>
            <a-form-model-item prop="newPassword2">
              <a-input-password
                size="large"
                placeholder="请确认密码"
                autocomplete="autocomplete"
                v-model="form.newPassword2"
              >
              </a-input-password>
            </a-form-model-item>
          </a-tab-pane>
        </a-tabs>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%; margin-top: 24px"
            size="large"
            @click="reset('changePwdForm')"
            htmlType="submit"
            type="primary"
            >确定</a-button
          >
        </a-form-item>
        <a-form-item>
          <a-button
            style="width: 100%;"
            class="back-btn"
            size="large"
            @click="goLogin"
            htmlType="submit"
            >返回登录</a-button
          >
        </a-form-item>
      </a-form-model>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from "@/layouts/CommonLayout";
import { changePwd } from "@/services/user";
import { rule } from "@/utils/validateRules";
import { mapMutations } from "vuex";
import { encrypt } from "@/utils/util";


export default {
  name: "Login",
  components: { CommonLayout },
  data() {
    var validatePass2 = (rule, value, callback) => {
      try {
        if (value !== this.form.newPassword) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      } catch (error) {
        callback(new Error("密码至少包含大小写字母、数字、符号中的三种!"));
      }
    };
    return {
      logging: false,
      error: "",
      form: {
        newPassword: "",
        newPassword2: ""
      },
      rules: {
        newPassword: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 8, message: "密码长度至少为8位", trigger: ["change", "blur"] },
          { trigger: ["change", "blur"], validator: rule.validatePwd }
        ],
        newPassword2: [
          { required: true, validator: validatePass2, trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.$message.info("请先修改密码", 2);
  },
  computed: {
    ...mapMutations("account", ["setPermissions", "setIconMap"])
  },
  methods: {
    goLogin() {
      this.$router.push("login");
    },
    reset(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.logging = true;
          changePwd(encrypt(this.form.newPassword), 1).then(this.afterChangePwd);
        } else {
          return false;
        }
      });
    },
    afterChangePwd(res) {
      this.logging = false;
      const loginRes = res.data;
      if (loginRes.code == 0) {
        this.$message.info("密码修改成功", 2);
        setTimeout(() => {
          this.$router.push("login");
        }, 1000);
      } else {
        this.error = loginRes.message;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: "Myriad Pro", "Helvetica Neue", Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    .captcha-button {
      color: #fff;
    }
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
    /deep/ .ant-input {
      height: 42px;
      font-size: 14px;
    }
    .ant-btn {
      background-color: #2979e7;
      border-radius: 0;
      &:hover {
        background-color: #5494ec !important;
        border-color: #5494ec !important;
      }
    }
    .back-btn {
      color: #425067 !important;
      background-color: #f6f8f9 !important;
      border-color: #d8dee5 !important;
      &:hover {
        background-color: #f3f6f9 !important;
        border-color: #e0e5ea !important;
      }
    }
  }
}
</style>
